package com.zhika.zishi.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.zhika.zishi.R;
import com.zhika.zishi.adapter.FragmentSelfSPAdapter;
import com.zhika.zishi.customview.TitleView;
import com.zhika.zishi.fragments.NewStrategyFragment;
import com.zhika.zishi.interfaces.TitleInterface;
import com.zhika.zishi.util.ActivityCollector;
import com.zhika.zishi.util.MyViewPagerAnimation;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/27.
 */
public class NewStrategyActivity extends FragmentActivity {

    private TitleView titleView;

    private ViewPager viewPager;

    private Button newButton;

    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    //viewpager的适配器
    private FragmentSelfSPAdapter newStrategyAdapter;

    //整个布局的父布局
    private RelativeLayout relativeLayout;

    //viewpager的参数设置对象
    private RelativeLayout.LayoutParams viewPagerParams;

    //屏幕的宽
    private int screenWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_strategy_layout);
        ActivityCollector.addActivity(this);
        initView();
    }

    private void initView(){
        titleView = (TitleView) findViewById(R.id.new_strategy_title_view);
        titleView.setLeftButtonText("返回");
        titleView.setMiddleRowText("写攻略");
        titleView.setRightButtonText("确定");
        titleView.setTitleViewInterface(new TitleInterface() {
            @Override
            public void leftTitleListener() {
                finish();
            }

            @Override
            public void middleTitleListener() {

            }

            @Override
            public void rightTitleListener() {
                Intent intent = new Intent(NewStrategyActivity.this,SubmitStrategyActivity.class);
                startActivity(intent);
            }
        });

        //设置viewpager的宽，即每个fragment的宽
        screenWidth = getScreenSize();
        relativeLayout = (RelativeLayout) findViewById(R.id.new_strategy_layout);
        viewPager = (ViewPager) findViewById(R.id.new_strategy_view_pager);
        viewPagerParams = (RelativeLayout.LayoutParams) viewPager.getLayoutParams();
        relativeLayout.post(new Runnable() {
            @Override
            public void run() {
                viewPagerParams.width = screenWidth * 7 / 8;
                viewPager.setLayoutParams(viewPagerParams);
            }
        });

        fragmentList.add(new NewStrategyFragment());
        newStrategyAdapter = new FragmentSelfSPAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(newStrategyAdapter);

        viewPager.setPageTransformer(true,new MyViewPagerAnimation());
        viewPager.setOffscreenPageLimit(fragmentList.size());

        newButton = (Button) findViewById(R.id.new_strategy_new_button);
        newButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragmentList.add(new NewStrategyFragment());
                newStrategyAdapter.notifyDataSetChanged();
                viewPager.setCurrentItem(fragmentList.size()-1);
                viewPager.setOffscreenPageLimit(fragmentList.size());
            }
        });
    }

    private int getScreenSize(){
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int screenWidth = metrics.widthPixels;
        return screenWidth;
    }

    @Override
    protected void onDestroy() {
        ActivityCollector.remoteActivity(this);
        super.onDestroy();

    }
}
